<?php

namespace app\common\model;

use think\Model;


class UserAnswers extends Model
{


    // 表名
    protected $name = 'user_answers';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
//        'is_right_text'
    ];


    public function getIsRightList()
    {
        return ['0' => __('Is_right 0'), '1' => __('Is_right 1')];
    }


    public function getIsRightTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_right']) ? $data['is_right'] : '');
        $list = $this->getIsRightList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public static function setResult($project_id, $type, $user_id)
    {
        # 测评数据
        $list = collection(self::where(['user_id' => $user_id, 'project_id' => $project_id])->select())->toArray();
        if (empty($list)) {
            throw_validate_error(!$list, "测评结果不存在！");
        }
        # 测评答案
        $grading = Grading::where(['project_id' => $project_id])->select();
        if (empty($grading)) {
            throw_validate_error(!$grading, "测评答案后台未配置！");
        }
        # 查询答案
        $question = [];
        foreach ($list as &$v) {
            $questionName = Question::where(['id' => $v['question_id'], 'project_id' => $project_id])->value('name');
            $question[] = [
                'question_name' => $questionName,
                'answer' => $v['answer'],
                'score' => $v['score'],
                'inner' => $v['inner'],
            ];
        }
        $gradingData = [];
        switch ($type) {
            case 1:
                # 单题
                # 查询得出结果
                $answer = $score = $list[0]['answer'];
                collection($grading)->each(function ($v, $k) use ($answer, &$gradingData) {
                    if ($v['title'] == $answer) {
                        $gradingData = [
                            'grading_id' => $v['id'],
                            'acture_value' => $v['acture_value'], //答案表示
                            'basicdesc' => $v['basicdesc'], //基本表述
                        ];
                    }
                });
                break;
            case 2:
                # 计分题
                # 查询得出结果
                $score = array_sum(array_column($list, 'score'));
                collection($grading)->each(function ($v, $k) use ($score, &$gradingData) {
                    if ($v['min_score'] <= $score && $v['max_score'] >= $score) {
                        $gradingData = [
                            'grading_id' => $v['id'],
                            'acture_value' => $v['acture_value'], //答案表示
                            'basicdesc' => $v['basicdesc'], //基本表述
                        ];
                    }
                });
                break;
            case 3:
                # 跳跃题
                $redirect_question_id = $score = self::where(['user_id' => $user_id, 'project_id' => $project_id, 'trigger_type' => 'submit'])->value('redirect_question_id');
                if (!$redirect_question_id) {
                    throw_validate_error(!$gradingData, "结果生成失败，算法函数不存在！");
                }
                collection($grading)->each(function ($v, $k) use ($redirect_question_id, &$gradingData) {
                    if ($redirect_question_id == $v['title']) {
                        $gradingData = [
                            'grading_id' => $v['id'],
                            'acture_value' => $v['acture_value'], //答案表示
                            'basicdesc' => $v['basicdesc'], //基本表述
                        ];
                    }
                });
                break;
            case 4:
                # 选项多
                break;
        }
        # 数据整理
        if (empty($gradingData)) {
            throw_validate_error(!$gradingData, "结果生成失败，算法函数不存在！");
        }
        $userRecordInfo = UserRecord::where(['user_id' => $user_id, 'project_id' => $project_id])->find();
        if (!empty($userRecordInfo)) {
            return $userRecordInfo;
        }
        $result_json = [
            'question' => $question,
            'grading' => $gradingData,
        ];
        $data = [
            'user_id' => $user_id,
            'project_id' => $project_id,
            'grading_id' => $gradingData['grading_id'],
            'score' => $score,
            'answer' => $gradingData['acture_value'],
            'result_json' => empty($result_json) ? '' : json_encode($result_json),
            'createtime' => time(),
        ];
        $dataRs = UserRecord::create($data);
        return $dataRs;

    }


}
